package com.dawnling.greendaodemo.converter;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.util.Arrays;
import java.util.List;

/**
 * GreenDao：
 * List<String>字符串数组转换器
 */
public class StringListConverter implements PropertyConverter<List<String>, String> {

    private static final String SPLIT = "##gd##";
    @Override
    public List<String> convertToEntityProperty(String databaseValue) {
        if (databaseValue != null) return Arrays.asList(databaseValue.split(SPLIT));
        return null;
    }

    @Override
    public String convertToDatabaseValue(List<String> entityProperty) {
        if (entityProperty != null){
            if (entityProperty.size() == 0) return "";
            StringBuilder builder = new StringBuilder();
            builder.append(entityProperty.get(0));
            for(int i = 1; i < entityProperty.size(); i++){
                builder.append(SPLIT).append(entityProperty.get(i));
            }
            return builder.toString();
        }
        return null;
    }
}
